package com.shangze.test.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yjm on 2017/10/16.
 */

public abstract class BaseFragment<P extends BasePresenter> extends BaseToolbarTitleFragemnt implements BaseView {

    public P presenter;
    // 进程提示框
    private ProgressDialog pDialog;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mRootView = inflater.inflate(provideContentViewId(), container, false);
        unbinder = ButterKnife.bind(this, mRootView);//绑定framgent
        return mRootView;
    }
    /**
     * 数据处理
     */
    public abstract void initView();

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView(this);
        }
        initView();
    }

    protected abstract int provideContentViewId();

    public P createPresenter() {
        return null;
    }

    /**
     * 进程提示框
     *
     * @param noticeString 显示的提示消息
     */
    public void showProgressDialog(String noticeString, boolean cancelable) {
        dialogDismiss();
        pDialog = new ProgressDialog(getContext());
        pDialog.setCancelable(cancelable);
        pDialog.setMessage(noticeString);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.show();
        pDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                pDialog = null;
            }
        });
        pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                pDialog = null;
            }
        });
    }

    /**
     * 让提示框框消失
     */
    public void dialogDismiss() {
        if (pDialog != null) {
            pDialog.dismiss();
        }
    }


    @Override
    public void startActivity(Class clazz) {
        startActivity(new Intent(getContext(), clazz));
    }

    @Override
    public void startActivityForResult(Class clazz, int requestCode) {
        startActivityForResult(new Intent(getContext(), clazz), requestCode);
    }

    @Override
    public void startActivityForResult(Class clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getContext(), clazz);
        intent.putExtra("data", bundle);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void startActivity(Class clazz, Bundle bundle) {
        startActivity(new Intent(getContext(), clazz).putExtra("data", bundle));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public Context getContext() {
        return getActivity();
    }

    {
    }

    public void finishActivity() {

    }

}
